package com.ywyt.route;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.ywyt.util.ConnUtil;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class RouteProvider {
    public static final String EXCHANGE_NAME = "exchange_name";
    /*
    * routingKey
    * */
    public static final String ERROR = "route_error";
    public static final String DEBUG = "route_debug";
    public static final String INFO = "route_info";

    @Test
    public void publish() throws IOException, TimeoutException {
        //1.获取连接对象
        Connection connection = ConnUtil.getConnection();
        //2.获取channel
        Channel channel = connection.createChannel();
        //3.声明exchange
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        //4.发布消息
        /*
        * 1.exchange名称
        * 2.routingKey
        * */
        channel.basicPublish(EXCHANGE_NAME,ERROR,null,"error消息".getBytes());
        channel.basicPublish(EXCHANGE_NAME,DEBUG,null,"debug消息".getBytes());
        //channel.basicPublish(EXCHANGE_NAME,INFO,null,"info消息".getBytes());
        //关闭资源
        channel.close();
        connection.close();
    }
}
